############################################################################################################ # # DHCP Migration Script 3.11.2. (c) Radio IP Software 2015 # # Script to create a batch file that will contain netsh commands to # update DHCP server with scope information contained in CSV file # and to update client information contained in CSV file. # # Format of CSV scope file # Scope Name, IP Address, NetMask, Network ID, Description # # Format of CSV client file # Client Name, IP Address, NetMask, DNS1, DNS2, MAC Address, Lease Status # # # Syntax: # DHCPMigrationScript CSVScopeFileName CSVClientFileName DhcpServerIpAddress CommandOutputFileName LeaseDurationToUse # # where # # CSVScopeFileName is CSV file name containing scopes information # CSVClientFileName is CSV file name containing clients information # DhcpServerIpAddress is the address of the DHCP server to update # CommandOutputFileName is the file name used for output # LeaseDurationToUse is an optional value for default lease duration # ############################################################################################################ param([string] $CSVScopeFileName, [string] $CSVClientFileName, [string] $DhcpServerIpAddress, [string] $CommandOutputFileName, [string] $LeaseDurationToUse) Set-Variable defaultLeaseDuration -option Constant -value 691200 Set-Variable setRouterOptionID -option Constant -value 3 Set-Variable setLeaseDurationOptionID -option Constant -value 51 [string] $scopeCSVFileName = $CSVScopeFileName [string] $clientCSVFileName = $CSVClientFileName [string] $dhcpServer = $DhcpServerIpAddress [string] $cmdFileName = $CommandOutputFileName [string] $leaseDuration = $LeaseDurationToUse [object] $importScopesList = $null [object] $importClientsList = $null Function DisplayScriptSyntax { write-host @" Syntax: DHCPMigrationScript CSVScopeFileName CSVClientFileName DhcpServerIpAddress CommandOutputFileName [LeaseDurationToUse] where CSVScopeFileName is CSV file name containing scopes information CSVClientFileName is CSV file name containing clients information DhcpServerIpAddress is the IP address of the DHCP server to update CommandOutputFileName is the file name used for output LeaseDurationToUse is an optional value for default lease duration "@ } Function DisplayScriptContent { Write-Host -ForegroundColor Red @" DHCP Migration Script 3.11.2 loaded. (c) Radio IP Software 2015 "@ write-host -ForegroundColor White @" Available function: dhcp-CreateScript Creates a batch file that will contain netsh commands to update DHCP server with scope information contained in CSV file and client information contained in CSV file. "@ } Function AddDHCPScope($ScopeName, $NetworkID, $NetMask, $Description) { write-host Adding scope [$ScopeName] Network ID [$NetworkID] mask [$NetMask] Description [$Description] on DHCP Server [$dhcpServer] write-host COMMAND : netsh dhcp server $dhcpServer add scope $NetworkID $NetMask $ScopeName $Description add-content $cmdFileName "netsh dhcp server $dhcpServer add scope $NetworkID $NetMask $ScopeName $Description" } Function AddDHCPRangeForScope($NetworkID, $NetMask) { write-host Adding range for scope [$NetworkID] mask [$NetMask] on DHCP Server [$dhcpServer] [uint32] $ip = IpAddressToInt $NetworkID [uint32] $mask = IpAddressToInt $NetMask [uint32] $startAddress = ($ip + 1) -band [Uint32]::MaxValue [uint32] $endAddress = (($ip -bor $(-bnot $mask)) - 1) -band [Uint32]::MaxValue [string] $startAddressString = IpAddressToString($startAddress) [string] $endAddressString = IpAddressToString($endAddress) write-host COMMAND : netsh dhcp server $dhcpServer scope $NetworkID add iprange $startAddressString $endAddressString add-content $cmdFileName "netsh dhcp server $dhcpServer scope $NetworkID add iprange $startAddressString $endAddressString" } Function ExcludeIpFromRange($NetworkID, $IpAddress) { write-host Excluding IP [$IpAddress] for scope [$NetworkID] on DHCP Server [$dhcpServer] write-host COMMAND : netsh dhcp server $dhcpServer scope $NetworkID add excluderange $IpAddress $IpAddress add-content $cmdFileName "netsh dhcp server $dhcpServer scope $NetworkID add excluderange $IpAddress $IpAddress" } Function SetRouterOption($NetworkID, $IpAddress) { write-host Setting router option for IP [$IpAddress] for scope [$NetworkID] on DHCP Server [$dhcpServer] write-host COMMAND : netsh dhcp server $dhcpServer scope $NetworkID set optionvalue $setRouterOptionID IPADDRESS $IpAddress add-content $cmdFileName "netsh dhcp server $dhcpServer scope $NetworkID set optionvalue $setRouterOptionID IPADDRESS $IpAddress" } Function SetLeaseDuration($NetworkID, $leaseDuration) { write-host Setting lease duration to [$leaseDuration] for scope [$NetworkID] on DHCP Server [$dhcpServer] write-host COMMAND : netsh dhcp server $dhcpServer scope $NetworkID set optionvalue $setLeaseDurationOptionID DWORD $leaseDuration add-content $cmdFileName "netsh dhcp server $dhcpServer scope $NetworkID set optionvalue $setLeaseDurationOptionID DWORD $leaseDuration" } Function AddCmdFileAddScope() { write-host write-host Adding scopes -foregroundcolor blue -backgroundcolor black write-host add-content $cmdFileName "rem" add-content $cmdFileName "rem Adding scopes" add-content $cmdFileName "rem" } Function AddScope() { AddCmdFileAddScope # Step through Each Item in the Scope List foreach ($scope in $importScopesList) { if ( $scope.'Scope Name' ) { AddDHCPScope $scope.'Scope Name' $scope.'Network ID' $scope.'NetMask' $scope.'Description' AddDHCPRangeForScope $scope.'Network ID' $scope.'NetMask' ExcludeIpFromRange $scope.'Network ID' $scope.'IP Address' SetRouterOption $scope.'Network ID' $scope.'IP Address' if ( !$leaseDuration ) { SetLeaseDuration $scope.'Network ID' $defaultLeaseDuration } else { SetLeaseDuration $scope.'Network ID' $leaseDuration } } } } # IP Address network order change Function HTONL([uint32] $ipAddress) { return [int32] ([Convert]::ToInt64([IPAddress]::NetworkToHostOrder($ipAddress)) / ([Uint32]::MaxValue + 1)) -band [Uint32]::MaxValue } # IP Address converted and returned in host order Function IpAddressToInt([string] $IpAddress) { [IPAddress] $ip = $IpAddress [uint32] $ipToByte= [BitConverter]::ToInt32($ip.GetAddressBytes(), 0) -band [Uint32]::MaxValue return HTONL $ipToByte } # IP Address converted and returned in string Function IpAddressToString([uint32] $IpAddress) { [IPAddress] $ip = HTONL $IpAddress return [string] $ip.ToString() } # Remove all - from mac address Function RemoveDashFromMac([string] $MacAddress) { return [string] $MacAddress -replace "-", "" } Function ReservedClientAddressInDHCPScope($ClientName, $IpAddress, $NetMask, $MacAddress) { [uint32] $ip = IpAddressToInt $IpAddress [uint32] $mask = IpAddressToInt $NetMask [uint32] $NetworkID = $ip -band $mask [string] $NetworkIDString = IpAddressToString($NetworkID) write-host Adding client [$ClientName] with IP [$IPAddress], Netmask [$NetMask], MAC [$MacAddress] to reservations for scope [$NetworkIDString] on DHCP Server [$dhcpServer] [string] $strippedMac = RemoveDashFromMac $MacAddress write-host netsh dhcp server $dhcpServer scope $NetworkIDString add reservedip $IpAddress $strippedMac $ClientName add-content $cmdFileName "netsh dhcp server $dhcpServer scope $NetworkIDString add reservedip $IpAddress $strippedMac $ClientName" } Function AddCmdFileClientsToScopeText { write-host write-host Adding clients -foregroundcolor blue -backgroundcolor black write-host add-content $cmdFileName "rem" add-content $cmdFileName "rem Adding clients to scopes" add-content $cmdFileName "rem" } Function AddClientToScope() { AddCmdFileClientsToScopeText # Step through Each Item in the List foreach ($client in $importClientsList) { if ( $client.'Client Name' ) { if ( $client.'Lease Status' -eq "Reserved" ) { ReservedClientAddressInDHCPScope $client.'Client Name' $client.'IP Address' $client.'NetMask' $client.'MAC Address' $client.'Lease Status' } } } } Function AddCmdFileHeaderText() { write-host write-host Starting script with parameters [$scopeCSVFileName] [$clientCSVFileName], [$dhcpServer] [$cmdFileName] [$leaseDuration] -foregroundcolor yellow -backgroundcolor black write-host add-content $cmdFileName "rem ######################################################################" add-content $cmdFileName "rem Creating command file name for DHCP Server [$dhcpServer]" add-content $cmdFileName "rem ######################################################################" add-content $cmdFileName "rem" } Function AddCmdFileFooterText() { add-content $cmdFileName "rem" add-content $cmdFileName "rem ######################################################################" add-content $cmdFileName "rem Commands are ready to update DHCP Server [$dhcpServer]" add-content $cmdFileName "rem Make sure you have sufficient privileges to modify DHCP server" add-content $cmdFileName "rem ######################################################################" write-host write-host End of script -foregroundcolor yellow -backgroundcolor black write-host } Function CheckParameters() { if ( !$scopeCSVFileName -or !$clientCSVFileName -or !$dhcpServer -or !$cmdFileName ) { return $false } return $true } Function RunScript { # Import list of scopes or clients from CSV $importScopesList=IMPORT-CSV $scopeCSVFileName $importClientsList=IMPORT-CSV $clientCSVFileName # Make sure file is empty if ( $cmdFileName -and $(Test-path $cmdFileName) ) { clear-content $cmdFileName } AddCmdFileHeaderText AddScope AddClientToScope AddCmdFileFooterText Start-Process $cmdFileName } Function global:dhcp-CreateScript([Parameter(Mandatory=$true)] [string] $CSVScopeFileName, [Parameter(Mandatory=$true)] [string] $CSVClientFileName, [Parameter(Mandatory=$true)] [string] $DhcpServerIpAddress, [Parameter(Mandatory=$true)] [string] $CommandOutputFileName, $LeaseDurationToUse) { $scopeCSVFileName = $CSVScopeFileName $clientCSVFileName = $CSVClientFileName $dhcpServer = $DhcpServerIpAddress $cmdFileName = $CommandOutputFileName $leaseDuration = $LeaseDurationToUse RunScript } switch ($psboundparameters.count) { 0 {DisplayScriptContent} 4 {RunScript} 5 {RunScript} default {DisplayScriptSyntax} }